package com.japp.manager.web;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.japp.base.BaseAction;
import com.japp.manager.bean.RiskDealBean;
import com.japp.manager.service.RiskDealService;
import com.japp.sys.bean.SelectBean;
import com.japp.sys.mapper.DpMapper;
import com.japp.tools.common.Common;
import com.japp.tools.common.JappLog;

/**
 * 风控导入报告
 * 
 * @author Administrator
 * 
 */
@Scope("prototype")
@Controller
public class RiskDealAction extends BaseAction<RiskDealBean> {

	private static final long serialVersionUID = -5677670163048708848L;
	@Autowired
	private RiskDealService rdService;
	@Autowired
	private DpMapper dpMapper;
	private RiskDealBean rd = new RiskDealBean();
	private File uploadify;
	private String uploadifyFileName;
	private String[] uploadifyContentType;
	private List<RiskDealBean> list = new ArrayList<>();
	private Map<String, String> map = null;

	/**
	 * 查询风控导入报告
	 * 
	 * @return
	 */
	public String queryRiskDeal() {
		try {
			List<SelectBean> list = dpMapper.queryDpSelectAll(); // 部门
			json_val = Common.formtSelectToJson(list);
		} catch (Exception e) {
			JappLog.log.error("查询部门或转换Json错误", e);
		}
		page = rdService.getPage(page, rd);
		return SUCCESS;
	}

	/**
	 * 查看
	 * 
	 * @return
	 */
	public String findRiskDealView() {
		if (rd.getId() != 0) {
			rd = rdService.findRiskDealById(rd.getId());
		}
		return "rdView";
	}

	/**
	 * 欲新增 以及更新
	 * 
	 * @return rdView rdDetil
	 */
	public String findRiskDealDetil() {
		if (rd.getId() != 0) {
			rd = rdService.findRiskDealById(rd.getId());
		}
		return "dataDetil";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public void deleteRiskDealById() {
		int rel = rdService.deleteRiskDeal(keys);
		if (rel > 0) {
			result = "删除成功!";
		} else {
			result = "删除失败!";
		}
		wPrint(result);
	}

	/**
	 * 更新
	 * 
	 * @return
	 */
	public void updateRiskDealById() {
		int rel = rdService.updateRiskDeal(rd);
		if (rel > 0) {
			result = String.valueOf(rd.getId());
		} else {
			result = "error";
		}
		wPrint(result);
	}

	/**
	 * 新增
	 */
	public void insertRiskDealById() {
		int rel = rdService.insertRiskDealList(list);
		if (rel > 0) {
			result = String.valueOf(rel);
		} else {
			result = "0";
		}
		wPrint(result);
	}

	public void uploadExcel() {
		int rows = 0; 	// 表单行数
		Sheet sh = null; // 表单对象/工作表
		Row xr = null;
		int shCont = 0; // 工作表数量
		Cell cell;
		BigDecimal x3 = BigDecimal.ZERO,x4 = BigDecimal.ZERO,x5 = BigDecimal.ZERO,x6 = BigDecimal.ZERO,x7 = BigDecimal.ZERO,x8 = BigDecimal.ZERO,x9 = BigDecimal.ZERO;
		if (uploadify != null) {
			JSONArray ja = null;
			//PrintWriter out = null;
			List<String> list_code = new ArrayList<String>();
			SimpleDateFormat sf = new SimpleDateFormat("yyMMdd");
			SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				System.out.println(uploadifyFileName);
				System.out.println(uploadifyContentType);
				Workbook wb = Common.getWorkbook(uploadifyFileName, uploadify);// 得到工作薄对象
				shCont = wb.getNumberOfSheets();
				String submit_date = "",submit_ucode = "",submit_uname = "",sheetName = "";
				for (int x = 0; x < shCont; x++) {// 循环所有表
					rd = new RiskDealBean();
					sh = wb.getSheetAt(x);// wb.getSheetAt(x);
					// name = wb.getSheetName(x);
					sheetName = wb.getSheetName(x);
					sheetName = sheetName.substring(sheetName.length()-6,sheetName.length());
					submit_date = sf1.format(sf.parse(sheetName));
					if (sh == null) {
						continue;
					}
					rows = sh.getPhysicalNumberOfRows() -1;
					for(int i=2;i<rows;i++){
						//x3 = 0.00; x4 = 0.00;x5 = 0.00; x6 = 0.00;x7 = 0.00;x8 = 0.00;x9 = 0.00;
						xr = sh.getRow(i);
						if(xr==null||xr.getCell(0)==null){
							continue;
						}
						try {
							submit_ucode = xr.getCell(2).getStringCellValue();	//经纪人代码(提交人)
							submit_uname = xr.getCell(14).getStringCellValue();	//经纪人代码(提交人)
							cell = xr.getCell(5);
							if(cell!=null&&!"".equals(cell.toString().trim())){
								x3 = BigDecimal.valueOf(cell.getNumericCellValue());	//手续费
							}
							cell = xr.getCell(6);
							if(cell!=null&&!"".equals(cell.toString().trim())){
								x5 = BigDecimal.valueOf(cell.getNumericCellValue());	//留存手续费(手续费收入)
							}
							x4 = x3.subtract(x5);		//上交手续费 = 手续费-留存手续费
							
							cell = xr.getCell(7);
							if(cell!=null&&!"".equals(cell.toString().trim())){
								x6 = BigDecimal.valueOf(cell.getNumericCellValue());	//交易额
							}
							cell = xr.getCell(11);
							if(cell!=null&&!"".equals(cell.toString().trim())){
								x7 = BigDecimal.valueOf(cell.getNumericCellValue());	//客户权益
							}
							cell = xr.getCell(10);
							if(cell!=null&&!"".equals(cell.toString().trim())){
								x8 = BigDecimal.valueOf(cell.getNumericCellValue());	//可用资金
							}
							cell = xr.getCell(12);
							if(cell!=null&&!"".equals(cell.toString().trim())){
								x9 = BigDecimal.valueOf(cell.getNumericCellValue());	//总亏嬴
							}
							if (x3.compareTo(BigDecimal.ZERO) !=0
									|| x4.compareTo(BigDecimal.ZERO) !=0
									|| x5.compareTo(BigDecimal.ZERO) !=0
									|| x6.compareTo(BigDecimal.ZERO) !=0
									|| x7.compareTo(BigDecimal.ZERO) !=0
									|| x8.compareTo(BigDecimal.ZERO) !=0
									|| x9.compareTo(BigDecimal.ZERO) !=0) {
								rd = new RiskDealBean();
								rd.setSxf(x3);
								rd.setSjsxf(x4);
								rd.setSxfsr(x5);
								rd.setJre(x6);
								rd.setKhqy(x7);
								rd.setKyzj(x8);
								rd.setZky(x9);
								rd.setSubmit_date(submit_date);
								rd.setSubmit_ucode(submit_ucode);
								rd.setSubmit_uname(submit_uname);
								list_code.add(submit_ucode);
								list.add(rd);
							}
						} catch (Exception e) {
							rd.setBool(false);
							rd.setError(uploadifyFileName+"中，弟"+String.valueOf(i+1)+"行数据解析请检查,否则该条数据无法正常导入!");
							list.add(rd);
							continue;
						}
					}						
				}
				Set<String> set = null;
				if(list_code!=null&&list_code.size()>0){
					set = new HashSet<>(list_code);
					map = rdService.getUserByUcode(set);
				}
				//out = response.getWriter();
				Map<String, Object> maps = new HashMap<String, Object>();
				maps.put("rdMap", map);
				maps.put("rdList", list);
				ja = JSONArray.fromObject(maps);
				wPrint(ja.toString());
			} catch (Exception e) {
				wPrint("ERROR");
				e.printStackTrace();
			} 
		}
	}

	public RiskDealBean getRd() {
		return rd;
	}

	public void setRd(RiskDealBean rd) {
		this.rd = rd;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public File getUploadify() {
		return uploadify;
	}

	public void setUploadify(File uploadify) {
		this.uploadify = uploadify;
	}

	public String getUploadifyFileName() {
		return uploadifyFileName;
	}

	public void setUploadifyFileName(String uploadifyFileName) {
		this.uploadifyFileName = uploadifyFileName;
	}

	public String[] getUploadifyContentType() {
		return uploadifyContentType;
	}

	public void setUploadifyContentType(String[] uploadifyContentType) {
		this.uploadifyContentType = uploadifyContentType;
	}

	public List<RiskDealBean> getList() {
		return list;
	}

	public void setList(List<RiskDealBean> list) {
		this.list = list;
	}

}
